<route lang="json5" type="home">
{
  style: {
    navigationStyle: 'default',
    navigationBarTitleText: '我的',
  },
}
</route>
<script setup lang="ts">
import { useAuthStore } from '@/store/auth'
import { getUserInfo } from '@/service/user/user'
import type { PrivateUserInfo } from '@/service/user/type'
import { onShow } from '@dcloudio/uni-app'
// import { getCountUnread } from '@/service/message/message'

const useAuth = useAuthStore()
const toReservationRecord = () => {
  if (!useAuth.getToken()) {
    uni.$uv.route({
      url: '/pages/login/login',
    })
    return
  }
  uni.$uv.route({
    url: '/pages/reservation-record/reservation-record',
  })
}

const toLogin = () => {
  uni.$uv.route({
    url: '/pages/login/login',
  })
}
const userInfo = ref<PrivateUserInfo>()
const loadUserInfo = async () => {
  if (!useAuth.getToken()) {
    return
  }
  const resData = await getUserInfo()
  console.log(resData)
  if (resData.code === 0) {
    userInfo.value = resData.data
  }
}
// const messageCount = ref(0)
// const loadMessageCount = async () => {
//   const resData = await getCountUnread()
//   messageCount.value = resData.data as number
// }
// const toMessagePage = () => {
//   uni.$uv.route('/pages/message/message')
// }
onShow(() => {
  loadUserInfo()
  // loadMessageCount()
})
</script>

<template>
  <view ml mr>
    <view mt2 flex v-if="useAuth.getToken()">
      <uv-avatar :src="userInfo.avatar" mode="aspectFill"></uv-avatar>
      <view>
        <uv-text :text="`昵称：${userInfo.nickName}`"></uv-text>
        <uv-text :text="`信誉分：${userInfo.creditworthy}`"></uv-text>
      </view>
      <!--      <view ml-auto center>-->
      <!--        <uv-tags :text="`消息`" type="primary" @click="toMessagePage"></uv-tags>-->
      <!--        <uv-badge-->
      <!--          type="error"-->
      <!--          max="99"-->
      <!--          :value="messageCount"-->
      <!--          :absolute="true"-->
      <!--          :offset="[10, 10]"-->
      <!--        ></uv-badge>-->
      <!--      </view>-->
    </view>
    <view mt2 v-else>
      <uv-button type="primary" @click="toLogin">登录</uv-button>
    </view>

    <view mt>
      <uv-list>
        <uv-list-item>
          <uv-cell icon="list" title="预定记录" is-link @click="toReservationRecord"></uv-cell>
          <uv-cell icon="setting" title="设置" is-link url="/pages/setting/setting"></uv-cell>
        </uv-list-item>
      </uv-list>
    </view>
  </view>
</template>

<style scoped lang="scss">
.min-max {
  min-width: 5rpx;
  max-width: 100rpx;
}

.box {
  width: 50px;
  height: 50px;
  background-color: #909193;
}
</style>
